const SortingAlgorithms = require("../template")

function testSortingAlgorithmsBasic() {
  const sorter = new SortingAlgorithms()

  console.log(
    "1. 冒泡排序基础测试:",
    (() => {
      const arr = [64, 34, 25, 12, 22, 11, 90]
      const result = sorter.bubbleSort([...arr])
      const expected = [11, 12, 22, 25, 34, 64, 90]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "2. 选择排序基础测试:",
    (() => {
      const arr = [64, 34, 25, 12, 22, 11, 90]
      const result = sorter.selectionSort([...arr])
      const expected = [11, 12, 22, 25, 34, 64, 90]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "3. 插入排序基础测试:",
    (() => {
      const arr = [64, 34, 25, 12, 22, 11, 90]
      const result = sorter.insertionSort([...arr])
      const expected = [11, 12, 22, 25, 34, 64, 90]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "4. 归并排序基础测试:",
    (() => {
      const arr = [64, 34, 25, 12, 22, 11, 90]
      const result = sorter.mergeSort([...arr])
      const expected = [11, 12, 22, 25, 34, 64, 90]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "5. 快速排序基础测试:",
    (() => {
      const arr = [64, 34, 25, 12, 22, 11, 90]
      const result = sorter.quickSort([...arr])
      const expected = [11, 12, 22, 25, 34, 64, 90]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "6. 堆排序基础测试:",
    (() => {
      const arr = [64, 34, 25, 12, 22, 11, 90]
      const result = sorter.heapSort([...arr])
      const expected = [11, 12, 22, 25, 34, 64, 90]
      return JSON.stringify(result) === JSON.stringify(expected)
    })()
  )

  console.log(
    "7. 空数组排序测试:",
    (() => {
      const result = sorter.bubbleSort([])
      return JSON.stringify(result) === JSON.stringify([])
    })()
  )

  console.log(
    "8. 单元素数组排序测试:",
    (() => {
      const result = sorter.bubbleSort([42])
      return JSON.stringify(result) === JSON.stringify([42])
    })()
  )

  console.log(
    "9. 已排序数组测试:",
    (() => {
      const arr = [1, 2, 3, 4, 5]
      const result = sorter.bubbleSort([...arr])
      return JSON.stringify(result) === JSON.stringify([1, 2, 3, 4, 5])
    })()
  )

  console.log(
    "10. 逆序数组测试:",
    (() => {
      const arr = [5, 4, 3, 2, 1]
      const result = sorter.bubbleSort([...arr])
      return JSON.stringify(result) === JSON.stringify([1, 2, 3, 4, 5])
    })()
  )

  console.log(
    "11. 重复元素数组测试:",
    (() => {
      const arr = [3, 1, 3, 2, 1, 2]
      const result = sorter.bubbleSort([...arr])
      return JSON.stringify(result) === JSON.stringify([1, 1, 2, 2, 3, 3])
    })()
  )

  console.log(
    "12. 负数数组排序测试:",
    (() => {
      const arr = [-3, -1, -4, -2, -5]
      const result = sorter.bubbleSort([...arr])
      return JSON.stringify(result) === JSON.stringify([-5, -4, -3, -2, -1])
    })()
  )

  console.log(
    "13. 混合正负数数组测试:",
    (() => {
      const arr = [-3, 1, -4, 2, -5, 0]
      const result = sorter.bubbleSort([...arr])
      return JSON.stringify(result) === JSON.stringify([-5, -4, -3, 0, 1, 2])
    })()
  )

  console.log(
    "14. this上下文排序测试:",
    (() => {
      const sortObj = {
        multiplier: 2,
        processSort: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.bubbleSort([...arr])
          return result.map(item => item * this.multiplier)
        }
      }
      const result = sortObj.processSort([3, 1, 2])
      return JSON.stringify(result) === JSON.stringify([2, 4, 6])
    })()
  )

  console.log(
    "15. 复杂对象this绑定测试:",
    (() => {
      const sortConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.selectionSort([...arr])
          return result.map(item => item + this.config.offset)
        }
      }
      const result = sortConfigObj.processWithConfig([2, 1, 3])
      return JSON.stringify(result) === JSON.stringify([6, 7, 8])
    })()
  )
}

testSortingAlgorithmsBasic()
